package app.Disthanitian.thaistyle.EmerMez;

import static android.provider.BaseColumns._ID;
import static app.Disthanitian.thaistyle.EmerMez.Constant.DESTINATIONLAT;
import static app.Disthanitian.thaistyle.EmerMez.Constant.DESTINATIONLNG;
import static app.Disthanitian.thaistyle.EmerMez.Constant.NAME;
import static app.Disthanitian.thaistyle.EmerMez.Constant.ORIGINLAT;
import static app.Disthanitian.thaistyle.EmerMez.Constant.ORIGINLNG;
import static app.Disthanitian.thaistyle.EmerMez.Constant.TABLE_NAME;
import static app.Disthanitian.thaistyle.EmerMez.Constant.TIMES;
import static app.Disthanitian.thaistyle.EmerMez.Constant.TYPE;

import java.util.ArrayList;

import com.google.android.gms.maps.GoogleMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.util.Log;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListRiskZoneAllCentralActivity extends Activity {
	SQLiteDatabase mdb;
	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private DatabaseConnection con;
	ArrayList<RiskZone> riskArea,myRiskArea;
	DatabaseConnection db;
	// ArrayList<LatLng> riskZones;
	String options[] = { "View", "Turn Off", "Cancel" };
	Cursor mCursor;
	Intent i;
	static RiskZone risk = null;
	String riskArray[];
	int index;
	ListView listView1;
	int positions;
	ArrayAdapter<String> dirAdapter;
	ArrayList<String> dirArray;
	RiskZone r;
	RiskZoneAll riskAll;
	int checkTunrOn;
	ListRiskZoneAdapter adapter;
	
    
    AdapterView.AdapterContextMenuInfo info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.list_riskzone);
		//listView1 = (ListView) findViewById(R.id.About);
		
		listView1 = (ListView) findViewById(R.id.listriskzone_view);
		//TextView textView = (TextView) findViewById(R.id.listriskzone_view);
		//textView.setText("All:Central");
		
		//riskArea = new ArrayList<RiskZone>();
		
		//riskArea = risk.getAllRiskZone(this);
		riskAll = new RiskZoneAll();
		risk = new RiskZone();
		riskArea = new ArrayList<RiskZone>();
		myRiskArea = new ArrayList<RiskZone>();
		
		riskAll.setVariable1(new MemberUser().getId(),"C","rating_avg");
		riskAll.callRunShowRiskAllByTypeOrderBy();
		while(riskAll.getRiskFromDB()==null){
			
		}	
		Log.d("TestRiskArea",""+riskAll.getRiskFromDB().size());
		riskArea = riskAll.getRiskFromDB();
		Log.d("riskAreaValue",""+riskArea.size());
		
		

		//dirArray = new ArrayList<String>();
		//riskArray = new String[riskArea.size()];
		/*int count = 0;
		for (RiskZone r : riskArea) {
			for(int i=0;i<myRiskArea.size();i++)
				if(r.getId().equals(myRiskArea.get(i).getId())){
					riskArea.get(count).setEnable(myRiskArea.get(i).getEnable());
					break;
			}
			dirArray.add(r.getSName() + "\n" + r.getDestinationLat());
			riskArray[count] = r.getId();
			count++;
		}*/

		/*dirAdapter = new ArrayAdapter<String>(getApplicationContext(),
				android.R.layout.simple_list_item_1, dirArray);*/
		 adapter = new ListRiskZoneAdapter(riskArea, this);
		listView1.setAdapter(adapter);
		i = new Intent(this, MainActivity.class);

		listView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				positions = position;
				index = (int) (id);
				openRiskZoneOptions();

			}
		});
	}

	public void showRiskZone() {

	}

	

	private void openRiskZoneOptions() {
		RiskZone r1 = (RiskZone)adapter.getItem(positions);
		Log.d("CheckEnable",""+r1.getEnable().toString());
		if(r1.getEnable().equals("F")){										
				
				options[1]= "Turn on";
		
			}else{
				
				options[1]="Turn off";
			}		
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Options");
		dialog.setItems(options,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						switch (which) {
						case 0:
							i.putExtra("RiskZoneId", riskArea.get(index).getId());
							Log.d("RiskZone", "" + riskArea.get(index).getId());
							startActivity(i);
							
							break;
						case 1:
							
							RiskZone r = (RiskZone)adapter.getItem(positions);	
							if(options[1].equals("Turn off")){
								riskAll.setVariableForUpdateRiskZoneEnable(new MemberUser().getId(),r.getId(),"F");
								riskAll.callRunUpdateRiskZoneEnable();								
								
								 
								 r.setEnable("F");
								 riskArea.remove(positions);
								 riskArea.add(positions, r);
								 adapter = new ListRiskZoneAdapter(riskArea, ListRiskZoneAllCentralActivity.this);
								 adapter.notifyDataSetChanged();
								 listView1.setAdapter(adapter);
								break;
								
							}else{
								riskAll.setVariableForUpdateRiskZoneEnable(new MemberUser().getId(),r.getId(),"T");
								riskAll.callRunUpdateRiskZoneEnable();
								r.setEnable("T");
								 riskArea.remove(positions);
								 riskArea.add(positions, r);
								 adapter = new ListRiskZoneAdapter(riskArea, ListRiskZoneAllCentralActivity.this);
								 adapter.notifyDataSetChanged();
								 listView1.setAdapter(adapter);
								 break;
							}
							//Toast.makeText(ListRiskZoneAllCentralActivity.this, ""+r.getId(), 3).show();				
						case 2:
							break;
						}

					}
				});
		dialog.show();

	}

	private void editRiskZone() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Title");
		alert.setMessage("Message");

		// Set an EditText view to get user input
		final EditText input = new EditText(this);
		//input.setText(r.getName());
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				Editable value = input.getText();
				String value1 = value.toString();
				RiskZone r1 = new RiskZone();
				r1 = r;
				/*r1.setName(value1);
				r.editRiskZone(ListRiskZoneAllCentralActivity.this, r);*/
				setListCallBack();
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

					}
				});

		alert.show();
	}

	public void setListCallBack() {
		listView1 = (ListView) findViewById(R.id.About);
		

		dirArray = new ArrayList<String>();
		riskArray = new String[riskArea.size()];
		int count = 0;
		for (RiskZone r : riskArea) {
			/*dirArray.add(r.getName() + "\n" + r.getDestinationLat());*/
			riskArray[count] = r.getId();
			count++;
		}

		dirAdapter = new ArrayAdapter<String>(getApplicationContext(),
				android.R.layout.simple_list_item_1, dirArray);

		listView1.setAdapter(dirAdapter);
		i = new Intent(this, MainActivity.class);

		listView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				positions = position;
				index = (int) (id);
				openRiskZoneOptions();

			}
		});
	}
	
	public void setVariable(){
		
	}
	

}
